﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ingebet.Model;
using Ingebet.Model.Domain;

namespace Ingebet.Web
{
    public partial class ModificationComptes : Page
    {
        private int? utilID;

        public int UtilID
        {
            get
            {
                if (utilID == null)
                {
                    string utilId = Request["UtilId"].ToString();
                    int id;
                    if (int.TryParse(utilId , out id))
                    {
                        utilID = id;
                    }
                }
                return utilID.Value;
            }
        }

        private Utilisateur util;

        public Utilisateur Util
        {
            get
            {
                if (util == null)
                {
                    Utilisateur u = Global.Container.Resolve<UtilisateurModel>().GetByID(UtilID);
                    if (u != null)
                        util = u;
                }
                return util;
            }
        }

        protected void Page_Load(object sender , EventArgs e)
        {
            if (!IsPostBack)
            {
                SetChamps();
            }
        }

        private void SetChamps()
        {
            this.txtNom.Text = Util.Nom;
            this.txtPrenom.Text = Util.Prenom;
            this.txtPass.Text = Util.Password;
            this.txtEmail.Text = Util.Email;
            this.txtNbrPoint.Text = Util.Solde.ToString();
            this.chkAdmin.Checked = Util.IsAdmin;
        }

        protected void btnEnregistrer_Click(object sender , EventArgs e)
        {
            Utilisateur user = Global.Container.Resolve<UtilisateurModel>().GetByID(UtilID);
            user.IsAdmin = this.chkAdmin.Checked;
            user.Nom = this.txtNom.Text;
            user.Prenom = this.txtPrenom.Text;
            user.Password = this.txtPass.Text;
            user.Email = this.txtEmail.Text;
            int solde;
            if (int.TryParse(this.txtNbrPoint.Text , out  solde))
            {
                user.Solde = solde;
            }
            else
            {
            }
            Global.Container.Resolve<UtilisateurModel>().Update(user);
            this.ClientScript.RegisterStartupScript(this.GetType() , "" , "window.parent.location.reload(); window.close();" , true);
        }

        protected void btnFermer_Click(object sender , EventArgs e)
        {
            this.ClientScript.RegisterStartupScript(this.GetType() , "" , "window.close();" , true);
        }
    }
}